package com.a.xposedemome;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;

import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class PrivacyService {
	private static final String TAG = "dhy_PrivacyService";
	private static final String INFO_DIR = "/data/system/xposedemo";
	private static boolean mRegistered = false;

	private static IPrivacyService mClient = null;
	private static final String cServiceName = "xposedemoMe";

	public static void register() {
		// Store secret and errors
		try {
			File file = new File(INFO_DIR);
			if (!file.exists()) {
				file.mkdirs();
			}
			Class<?> cServiceManager = Class
					.forName("android.os.ServiceManager");
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
				Method mAddService = cServiceManager.getDeclaredMethod(
						"addService", String.class, IBinder.class,
						boolean.class);
				mAddService.invoke(null, cServiceName, mPrivacyService, true);
			} else {
				Method mAddService = cServiceManager.getDeclaredMethod(
						"addService", String.class, IBinder.class);
				mAddService.invoke(null, cServiceName, mPrivacyService);
			}

			mRegistered = true;
		} catch (Exception e) {
			Log.e(TAG, "PrivacyService register");
			e.printStackTrace();
		}
	}

	public static boolean isRegistered() {
		return mRegistered;
	}

	public static IPrivacyService getClient() {
		// Runs client side
		if (mClient == null)
			try {
				// public static IBinder getService(String name)
				Class<?> cServiceManager = Class
						.forName("android.os.ServiceManager");
				Method mGetService = cServiceManager.getDeclaredMethod(
						"getService", String.class);
				mClient = IPrivacyService.Stub
						.asInterface((IBinder) mGetService.invoke(null,
								cServiceName));
			} catch (Exception e) {
				e.printStackTrace();
				Log.e(TAG, "getClient exception");
			}

		// Disable disk/network strict mode
		// TODO: hook setThreadPolicy
//		try {
//			ThreadPolicy oldPolicy = StrictMode.getThreadPolicy();
//			ThreadPolicy newpolicy = new ThreadPolicy.Builder(oldPolicy)
//					.permitDiskReads().permitDiskWrites().permitNetwork()
//					.build();
//			StrictMode.setThreadPolicy(newpolicy);
//		} catch (Throwable ex) {
//			// Util.bug(null, ex);
//		}

		return mClient;
	}

	private static final IPrivacyService.Stub mPrivacyService = new IPrivacyService.Stub() {

		@Override
		public String getValue(String id) throws RemoteException {
			File file = new File(INFO_DIR + File.separator + id);
			String valueString = null;
			if (!file.exists()) {
				return valueString;
			}
			File[] listFiles = file.listFiles();
			if (null == listFiles || listFiles.length == 0) {
				return valueString;
			}
			valueString = listFiles[0].getName();
			return valueString;
		}

		@Override
		public void setValue(String id, String value) throws RemoteException {
			Log.d(TAG, "setValue");
			File file = new File(INFO_DIR + File.separator + id);
			if (!file.exists()) {
				file.mkdirs();
			}
			File[] listFiles = file.listFiles();
			if (null != listFiles) {
				for (File file2 : listFiles) {
					file2.delete();
				}

			}
			File newFile = new File(file, value);
			try {
				newFile.createNewFile();
				Log.d(TAG, "path:" + newFile.getAbsolutePath());
			} catch (IOException e) {
				e.printStackTrace();
				Log.d(TAG, "Exception:" + e.toString());
			}
		}
	};

	public static void setValue(String id, String value) throws RemoteException {
		if (isRegistered())
			mPrivacyService.setValue(id, value);
		else {
			IPrivacyService client = getClient();
			if (client == null) {
				Log.d(TAG, "client is NULL");
			} else
				client.setValue(id, value);
		}
	}

	public static String getSetting(String id) throws RemoteException {
		if (isRegistered())
			return mPrivacyService.getValue(id);
		else {
			IPrivacyService client = getClient();
			if (client == null) {

				return id;
			} else
				return client.getValue(id);
		}
	}
}
